home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / gnu / gmp_1_3_.gz / gmp_1_3_ / gmp-1.3.2 / longlong.h < prev    next >
C/C++ Source or Header  |  1993-05-07  |  33KB  |  1,028 lines

  1. /* longlong.h -- definitions for mixed size 32/64 bit arithmetic.
  2.    Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
  3.  
  4.    This definition file is free software; you can redistribute it
  5.    and/or modify it under the terms of the GNU General Public
  6.    License as published by the Free Software Foundation; either
  7.    version 2, or (at your option) any later version.
  8.  
  9.    This definition file is distributed in the hope that it will be
  10.    useful, but WITHOUT ANY WARRANTY; without even the implied
  11.    warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12.    See the GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifndef LONG_TYPE_SIZE
  19. #ifdef BITS_PER_LONGINT
  20. #define LONG_TYPE_SIZE BITS_PER_LONGINT
  21. #else
  22. #define LONG_TYPE_SIZE 32
  23. #endif
  24. #endif
  25.  
  26. #define __BITS4 (LONG_TYPE_SIZE / 4)
  27. #define __ll_B (1L << (LONG_TYPE_SIZE / 2))
  28. #define __ll_lowpart(t) ((unsigned long int) (t) % __ll_B)
  29. #define __ll_highpart(t) ((unsigned long int) (t) / __ll_B)
  30.  
  31. /* Define auxiliary asm macros.
  32.  
  33.    1) umul_ppmm(high_prod, low_prod, multipler, multiplicand)
  34.    multiplies two unsigned long integers MULTIPLER and MULTIPLICAND,
  35.    and generates a two unsigned word product in HIGH_PROD and
  36.    LOW_PROD.
  37.  
  38.    2) __umulsidi3(a,b) multiplies two unsigned long integers A and B,
  39.    and returns a long long product.  This is just a variant of umul_ppmm.
  40.  
  41.    3) udiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  42.    denominator) divides a two-word unsigned integer, composed by the
  43.    integers HIGH_NUMERATOR and LOW_NUMERATOR, by DENOMINATOR and
  44.    places the quotient in QUOTIENT and the remainder in REMAINDER.
  45.    HIGH_NUMERATOR must be less than DENOMINATOR for correct operation.
  46.    If, in addition, the most significant bit of DENOMINATOR must be 1,
  47.    then the pre-processor symbol UDIV_NEEDS_NORMALIZATION is defined to 1.
  48.  
  49.    4) sdiv_qrnnd(quotient, remainder, high_numerator, low_numerator,
  50.    denominator).  Like udiv_qrnnd but the numbers are signed.  The
  51.    quotient is rounded towards 0.
  52.  
  53.    5) count_leading_zeros(count, x) counts the number of zero-bits from
  54.    the msb to the first non-zero bit.  This is the number of steps X
  55.    needs to be shifted left to set the msb.  Undefined for X == 0.
  56.  
  57.    6) add_ssaaaa(high_sum, low_sum, high_addend_1, low_addend_1,
  58.    high_addend_2, low_addend_2) adds two two-word unsigned integers,
  59.    composed by HIGH_ADDEND_1 and LOW_ADDEND_1, and HIGH_ADDEND_2 and
  60.    LOW_ADDEND_2 respectively.  The result is placed in HIGH_SUM and
  61.    LOW_SUM.  Overflow (i.e. carry out) is not stored anywhere, and is
  62.    lost.
  63.  
  64.    7) sub_ddmmss(high_difference, low_difference, high_minuend,
  65.    low_minuend, high_subtrahend, low_subtrahend) subtracts two
  66.    two-word unsigned integers, composed by HIGH_MINUEND_1 and
  67.    LOW_MINUEND_1, and HIGH_SUBTRAHEND_2 and LOW_SUBTRAHEND_2
  68.    respectively.  The result is placed in HIGH_DIFFERENCE and
  69.    LOW_DIFFERENCE.  Overflow (i.e. carry out) is not stored anywhere,
  70.    and is lost.
  71.  
  72.    If any of these macros are left undefined for a particular CPU,
  73.    C macros are used.  */
  74.  
  75. /* The CPUs come in alphabetical order below.
  76.  
  77.    Please add support for more CPUs here, or improve the current support
  78.    for the CPUs below!  */
  79.  
  80. #if defined (__GNUC__) && !defined (NO_ASM)
  81.  
  82. /* We sometimes need to clobber "cc" with gcc2, but that would not be
  83.    understood by gcc1.  Use cpp to avoid major code duplication.  */
  84. #if __GNUC__ < 2
  85. #define __CLOBBER_CC
  86. #define __AND_CLOBBER_CC
  87. #else /* __GNUC__ >= 2 */
  88. #define __CLOBBER_CC : "cc"
  89. #define __AND_CLOBBER_CC , "cc"
  90. #endif /* __GNUC__ < 2 */
  91.  
  92. #if defined (__a29k__) || defined (___AM29K__)
  93. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  94.   __asm__ ("add %1,%4,%5
  95.     addc %0,%2,%3"                            \
  96.        : "=r" ((unsigned long int)(sh)),                \
  97.         "=&r" ((unsigned long int)(sl))                \
  98.        : "%r" ((unsigned long int)(ah)),                \
  99.          "rI" ((unsigned long int)(bh)),                \
  100.          "%r" ((unsigned long int)(al)),                \
  101.          "rI" ((unsigned long int)(bl)))
  102. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  103.   __asm__ ("sub %1,%4,%5
  104.     subc %0,%2,%3"                            \
  105.        : "=r" ((unsigned long int)(sh)),                \
  106.          "=&r" ((unsigned long int)(sl))                \
  107.        : "r" ((unsigned long int)(ah)),                \
  108.          "rI" ((unsigned long int)(bh)),                \
  109.          "r" ((unsigned long int)(al)),                \
  110.          "rI" ((unsigned long int)(bl)))
  111. #define umul_ppmm(xh, xl, m0, m1) \
  112.   do {                                    \
  113.     unsigned long int __m0 = (m0), __m1 = (m1);                \
  114.     __asm__ ("multiplu %0,%1,%2"                    \
  115.          : "=r" ((unsigned long int)(xl))                \
  116.          : "r" (__m0),                        \
  117.            "r" (__m1));                        \
  118.     __asm__ ("multmu %0,%1,%2"                        \
  119.          : "=r" ((unsigned long int)(xh))                \
  120.          : "r" (__m0),                        \
  121.            "r" (__m1));                        \
  122.   } while (0)
  123. #define udiv_qrnnd(q, r, n1, n0, d) \
  124.   __asm__ ("dividu %0,%3,%4"                        \
  125.        : "=r" ((unsigned long int)(q)),                \
  126.          "=q" ((unsigned long int)(r))                \
  127.        : "1" ((unsigned long int)(n1)),                \
  128.          "r" ((unsigned long int)(n0)),                \
  129.          "r" ((unsigned long int)(d)))
  130. #define count_leading_zeros(count, x) \
  131.     __asm__ ("clz %0,%1"                        \
  132.          : "=r" ((unsigned long int)(count))            \
  133.          : "r" ((unsigned long int)(x)))
  134. #endif /* __a29k__ */
  135.  
  136. #if defined (__alpha__)
  137. #define umul_ppmm(ph, pl, m0, m1) \
  138.   do {                                    \
  139.     unsigned long int __m0 = (m0), __m1 = (m1);                \
  140.     __asm__ ("umulh %r1,%2,%0"                        \
  141.          : "=r" ((unsigned long int) ph)                \
  142.          : "%rJ" (__m0),                        \
  143.            "rI" (__m1));                        \
  144.     (pl) = (unsigned long int) (__m0) * (unsigned long int) (__m1);    \
  145.   } while (0)
  146. #define UMUL_TIME 46
  147. #define UDIV_TIME 500
  148. #endif
  149.  
  150. #if defined (__arm__)
  151. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  152.   __asm__ ("adds %1,%4,%5
  153.     adc %0,%2,%3"                            \
  154.        : "=r" ((unsigned long int)(sh)),                \
  155.          "=&r" ((unsigned long int)(sl))                \
  156.        : "%r" ((unsigned long int)(ah)),                \
  157.          "rI" ((unsigned long int)(bh)),                \
  158.          "%r" ((unsigned long int)(al)),                \
  159.          "rI" ((unsigned long int)(bl)))
  160. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  161.   __asm__ ("subs %1,%4,%5
  162.     sbc %0,%2,%3"                            \
  163.        : "=r" ((unsigned long int)(sh)),                \
  164.          "=&r" ((unsigned long int)(sl))                \
  165.        : "r" ((unsigned long int)(ah)),                \
  166.          "rI" ((unsigned long int)(bh)),                \
  167.          "r" ((unsigned long int)(al)),                \
  168.          "rI" ((unsigned long int)(bl)))
  169. #endif /* __arm__ */
  170.  
  171. #if defined (__gmicro__)
  172. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  173.   __asm__ ("add.w %5,%1
  174.     addx %3,%0"                            \
  175.        : "=g" ((unsigned long int)(sh)),                \
  176.          "=&g" ((unsigned long int)(sl))                \
  177.        : "%0" ((unsigned long int)(ah)),                \
  178.          "g" ((unsigned long int)(bh)),                \
  179.          "%1" ((unsigned long int)(al)),                \
  180.          "g" ((unsigned long int)(bl)))
  181. #define sub_ddmmss(sh, sl, ah, al, bh, bl) \
  182.   __asm__ ("sub.w %5,%1
  183.     subx %3,%0"                            \
  184.        : "=g" ((unsigned long int)(sh)),                \
  185.          "=&g" ((unsigned long int)(sl))                \
  186.        : "0" ((unsigned long int)(ah)),                \
  187.          "g" ((unsigned long int)(bh)),                \
  188.          "1" ((unsigned long int)(al)),                \
  189.          "g" ((unsigned long int)(bl)))
  190. #define umul_ppmm(ph, pl, m0, m1) \
  191.   __asm__ ("mulx %3,%0,%1"                        \
  192.        : "=g" ((unsigned long int)(ph)),                \
  193.          "=r" ((unsigned long int)(pl))                \
  194.        : "%0" ((unsigned long int)(m0)),                \
  195.          "g" ((unsigned long int)(m1)))
  196. #define udiv_qrnnd(q, r, nh, nl, d) \
  197.   __asm__ ("divx %4,%0,%1"                        \
  198.        : "=g" ((unsigned long int)(q)),                \
  199.          "=r" ((unsigned long int)(r))                \
  200.        : "1" ((unsigned long int)(nh)),                \
  201.          "0" ((unsigned long int)(nl)),                \
  202.          "g" ((unsigned long int)(d)))
  203. #define count_leading_zeros(count, x) \
  204.   __asm__ ("bsch/1 %1,%0"                        \
  205.        : "=g" (count)                        \
  206.        : "g" ((unsigned long int)(x)),                \
  207.          "0" (0UL))
  208. #endif
  209.  
  210. #if defined (__hppa)
  211. #define add_ssaaaa(sh, sl, ah, al, bh, bl) \
  212.   __asm__ ("add %4,%5,%1
  213.     addc %2,%3,%0"                            \
  214.        : "=r" ((unsigned long int)(sh)),                \
  215.          "=&r" ((unsigned long int)(sl))                \
  216.        : "%rM" ((unsigned long int)(ah)),                \
  217.          "rM" ((unsigned long int)(bh)),                \
  218.          "%rM" ((unsigned lo